import webpage.*;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Register
 */

public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String first_name = request.getParameter("first_name");
		String last_name = request.getParameter("last_name");
		String sex = request.getParameter("sex");
		String country = request.getParameter("country");
		String emailaddress = request.getParameter("emailaddress");
		String password = request.getParameter("password");
		String profilepic;
		if (sex.equals("male")){
			profilepic = "Image/male.jpg";
		}
		else {
			profilepic = "Image/female.jpg";
		}
		request.setAttribute("profilepic", profilepic);
		CreateDBConnection regist = new CreateDBConnection();
		String message = null;
		message = regist.register(first_name, last_name, sex, country,
				emailaddress, password, profilepic);
		request.setAttribute("message", message);
		System.out.println(request.getAttribute("message"));
		if (message.equals("Registration Successful")) {
			String path = "C:/SVN/WebContent/Members/"+emailaddress+"/";
			String fname= path+"friends.txt";
			String pname=path+"pendingrequests.txt";
			    File f = new File(path);
			    File f1 = new File(fname);
			    File f2 = new File(pname);

			    f.mkdirs() ;
			    try {
			        f1.createNewFile();
			        f2.createNewFile();
			    } catch (IOException e) {
			        e.printStackTrace();
			    }
			request.getRequestDispatcher("Home.jsp").forward(request, response);
		} else {
			request.getRequestDispatcher("retry.jsp")
					.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
